package com.arpa.ntocc.basic.api.controller;


import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.cache.PartyCacheDO;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.PartyBalanceStatisticsDTO;
import com.arpa.ntocc.common.domain.dto.PartyDTO;
import com.arpa.ntocc.common.domain.dto.PartySelectDTO;
import com.arpa.ntocc.common.domain.entity.Party;
import com.arpa.ntocc.common.domain.enums.PartyTypeEnum;
import com.arpa.ntocc.common.domain.vo.PartyBalanceStatisticsVo;
import com.arpa.ntocc.common.domain.vo.PartyVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.service.IPartyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户 Controller
 * </p>
 *
 * @author liuyj
 * @since 2019-11-25
 */
@Api(tags = "用户")
@RestController
@RequestMapping(value = "/party", produces = "application/json;charset=UTF-8")
@Log4j2
public class PartyController extends BaseController {

    @Autowired
    private IPartyService iPartyService;

    /**
     * 获取 用户 列表
     *
     * @param partyDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 用户 列表", notes = "获取 用户 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PartyVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(PartyDTO partyDTO, HttpServletRequest request) {
        //获取登录用户所属机构
        String groupCode =  UserUtil.getBranchCode();
        partyDTO.setBranchCode(groupCode);
        PartyVO partyVOSum = iPartyService.queryListSum(partyDTO);
        List<PartyVO> partyVOList = iPartyService.queryList(partyDTO);
        Page<PartyVO> returnPage = new Page<>(partyDTO.getPageNum(), partyDTO.getPageSize());
        returnPage.setRecords(partyVOList);
        returnPage.setTotal(partyVOSum.getTotal());
        returnPage.setSumObject(partyVOSum);
        return Result.ok().data(returnPage);
    }



    /**
     * 获取用户详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "用户code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Party.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result party(@PathVariable String code, HttpServletRequest request) {
        try {
            PartyVO entity = iPartyService.queryPartyInfo(code);
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }



    /**
     * 根据用户名模糊查询获取货主/发货企业用户列表
     *
     * @param name
     * @return
     */
    @ApiOperation(value = "根据用户名模糊查询获取仓储货主/发货企业用户列表", notes = "根据用户名模糊查询获取仓储货主/发货企业用户列表,只返回code和name,用于下拉选择")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "name", value = "name", paramType = "query", dataType = "String")
    })
    @GetMapping(value = "wmsShipment")
    public Result findWmsShipment(@RequestParam(required = false, defaultValue = "") String name) {
        try {
//            List<PartyCacheDO> partyCacheDOList = iPartyService.getPartyList(name,PartyTypeEnum.SHIPMENT.getValue(),PartyTypeEnum.SHIPMENT_COMPANY.getValue());
//            if(!partyCacheDOList.isEmpty()){
//                partyCacheDOList.stream().forEach(r->{
//                    r.setPhone("");
//                });
//            }
            List<Party> partyList = iPartyService.list(new QueryWrapper<Party>().lambda().in(Party::getPartyType, PartyTypeEnum.SHIPMENT.getValue(), PartyTypeEnum.SHIPMENT_COMPANY.getValue())
                    .eq(Party::getDeleted, 0));
            if(StringUtils.isNotBlank(name)){
                partyList = partyList.stream().filter(p->StringUtils.contains(p.getName(),name)).collect(Collectors.toList());
            }
            return Result.ok().data(partyList);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 用户余额统计
     * @param partyBalanceStatisticsDTO
     * @Date 2019/12/31 14:57
     * @Author wangkf
     * @return com.arpa.core.web.Result
    */
    @ApiOperation(value = "用户余额统计", notes = "用户余额统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PartyBalanceStatisticsVo.class))
    @GetMapping(value = "partyBalanceStatistics")
    public Result partyBalanceStatistics(PartyBalanceStatisticsDTO partyBalanceStatisticsDTO) {
        try {
            Page page = iPartyService.partyBalanceStatisticsPageList(partyBalanceStatisticsDTO);
            return Result.ok().data(page);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户下拉选
     *
     * @param partySelectDTO
     * @return
     */
    @ApiOperation(value = "获取用户列表，姓名手机号模糊搜索", notes = "获取用户下拉选,姓名手机号模糊搜索，可指定获取的用户类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @PostMapping(value = "selectUser")
    public Result selectUser(@RequestBody PartySelectDTO partySelectDTO) {
        List<PartyCacheDO> partyCacheDOList = iPartyService.selectUser(partySelectDTO);
        return Result.ok().data(partyCacheDOList);
    }
    /**
     * 获取当前登录用户详情
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取当前登录用户详情", notes = "获取当前登录用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Party.class))
    @NeedLogin
    @GetMapping(value = "/one")
    public Result getOne(HttpServletRequest request) {
        try {
            //抓取当前登陆用户code
            String partyCode = UserUtil.getCode();
            Party entity = iPartyService.getOne(new QueryWrapper<Party>().lambda().eq(Party::getCode, partyCode));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }
}
